package com.ymatou.iapi.liveseller.testcase;

import java.util.List;
import java.util.Map;
import java.util.Random;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.liveseller.parameter.SellerLiveDetailBean;
import com.ymatou.iapi.liveseller.service.SellerLiveDetailCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.livevideo.parameter.RecLiveNotifyBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 买手互动直播详情 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SellerLiveDetail {
	private static SellerLiveDetailBean sellerlivedetailBean;
	private static SellerLiveDetailCall sellerlivedetailCall;
	private static LiveRoomWapper liveRoomWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手互动直播详情");
	}

	@Before
	public void caseUp() {
		sellerlivedetailBean = new SellerLiveDetailBean();
		sellerlivedetailCall = new SellerLiveDetailCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerlivedetailCall.getOpurl());
		liveRoomWapper = null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDB(JsonObject livedto, int liveid) throws Exception {
		Logger.comment("数据库验证");
		LiveRoom dblive = liveRoomWapper.selectLiveRoomById(liveid);
		Logger.debug(FormatUtil.beanToGSONString(dblive));
		Logger.verifyEquals(livedto.get("EndTime").getAsString(), dblive.getEndTime().substring(0, 19), "验证EndTime");
		Logger.verifyEquals(livedto.get("GroupId").getAsString(), dblive.getGroupId(), "验证GroupId");
		Logger.verifyEquals(livedto.get("StartTime").getAsString(), dblive.getStartTime().substring(0, 19), "验证StartTime");
		Logger.verifyEquals(livedto.get("Latitude").getAsString(), dblive.getLatitude(), "验证Latitude");
		Logger.verifyEquals(livedto.get("Longitude").getAsString(), dblive.getLongitude(), "验证Longitude");
		Logger.verifyEquals(livedto.get("LiveAddress").getAsString(), dblive.getLiveAddress(), "验证LiveAddress");
		Logger.verifyEquals(livedto.get("LiveCover").getAsString(), dblive.getLiveCover(), "验证LiveCover");
		Logger.verifyEquals(livedto.get("LiveTitle").getAsString(), dblive.getLiveTitle(), "验证LiveTitle");
		Logger.verifyEquals(livedto.get("PreVideoHeight").getAsInt(), dblive.getPreVideoHeight(), "验证PreVideoHeight");
		Logger.verifyEquals(livedto.get("PreVideoLength").getAsInt(), dblive.getPreVideoLength(), "验证PreVideoLength");
		Logger.verifyEquals(livedto.get("PreVideoPic").getAsString(), dblive.getPreVideoPic(), "验证PreVideoPic");
		Logger.verifyEquals(livedto.get("PreVideoWidth").getAsInt(), dblive.getPreVideoWidth(), "验证PreVideoWidth");
		Logger.verifyEquals(livedto.get("SellerName").getAsString(), dblive.getSellerName(), "验证SellerName");
		Logger.verifyEquals(livedto.get("UserId").getAsInt(), dblive.getUserId(), "验证UserId");
		Logger.verifyEquals(livedto.get("PreVideoUrl").getAsString(), dblive.getPreVideoUrl(), "验证PreVideoUrl");
		Logger.verifyEquals(livedto.get("PushUrl").getAsString(), dblive.getPushUrl(), "验证PushUrl");
//		Logger.verifyEquals(livedto.get("PlayUrl").getAsString(), dblive.getPlayUrl(), "验证PlayUrl");
		Logger.verifyEquals(livedto.get("HistoryPlayUrl").getAsString(), dblive.getHistoryPlayUrl() == null ? "" : dblive.getHistoryPlayUrl(), "验证HistoryPlayUrl");
		Logger.verifyEquals(livedto.get("CreateTime").getAsString(), dblive.getCreateTime().substring(0, 19), "验证CreateTime");
		Logger.verifyEquals(livedto.get("UpdateTime").getAsString(), dblive.getUpdateTime().substring(0, 19), "验证UpdateTime");
		Logger.verifyEquals(livedto.get("StopReason").getAsString(), dblive.getStopReason(), "验证StopReason");
		Logger.verifyEquals(livedto.get("LivePush").getAsInt(), dblive.getLivePush(), "验证LivePush");

		String LiveStatusText = "";
		int livestatus = dblive.getLiveStatus();
		if (dblive.getLiveStatus() == 0 && dblive.getStartTime().compareTo(YMTDateUtil.getSysTime()) > 0) {
			LiveStatusText = "未开始";
		} else if (dblive.getLiveStatus() == 0 && dblive.getStartTime().compareTo(YMTDateUtil.getSysTime()) <= 0) {
			LiveStatusText = "待开始";
			livestatus = 1;
		} else if (dblive.getLiveStatus() == 2) {
			LiveStatusText = "进行中";
		} else if (dblive.getLiveStatus() == 3) {
			LiveStatusText = "进行中";
		} else if (dblive.getLiveStatus() == 7&&dblive.getLivePush()==0) {
			//未推流8 推流9
			livestatus = 8;
			LiveStatusText = "已关闭";
		} else if (dblive.getLiveStatus() == 7&&dblive.getLivePush()==1) {
			//未推流8 推流9
			livestatus = 9;
			LiveStatusText = "已结束";
		} else if (dblive.getLiveStatus() == 8) {
			LiveStatusText = "已关闭";
		} else if (dblive.getLiveStatus() == 9) {
			LiveStatusText = "已结束";
		} else if (dblive.getLiveStatus() == 10) {
			livestatus = 9;
			LiveStatusText = "已结束";
		}
		Logger.verifyEquals(livedto.get("LiveStatus").getAsInt(), livestatus, "验证LiveStatus");
		Logger.verifyEquals(livedto.get("LiveStatusText").getAsString(), LiveStatusText, "验证LiveStatusText");

		Map rstat = (Map) liveRoomWapper.selectLiveRoomStatById(liveid);
		Logger.debug(rstat);
		// "ViewNum":185,"OnLineNum":1,"LikeNum":0

	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_001() {
		Logger.start(true, "查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SellerLiveDetail_002() {
		Logger.start(true, "不输入liveid失败");
		try {
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("500", sellerlivedetailCall.getString("Code"), "验证Code");
			Logger.verifyIsContains("系统异常", sellerlivedetailCall.getString("Msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_003() {
		Logger.start(true, "直播状态2查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_004() {
		Logger.start(true, "直播状态3查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 3);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_005() {
		Logger.start(true, "直播状态7未推流查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			//未推流
//			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 7);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_006() {
		Logger.start(true, "直播状态8查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			//8是未推流的
//			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 8);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_007() {
		Logger.start(true, "直播状态9查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 9);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_008() {
		Logger.start(true, "直播状态10查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 10);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_009() {
		Logger.start(true, "直播结束回播地址查询正确");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			RecLiveNotifyBean recivenotifyBean = LiveRoomCallService.initRecLiveNotifyBean(createlive.getId(), 100);
			LiveRoomCallService.LiveNotify(recivenotifyBean);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_010() {
		Logger.start(true, "查询买手互动直播待开始");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			LiveRoomCallService.UpdateLiveTimeToStart(createlive.getId());
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveDetail_011() {
		Logger.start(true, "直播状态7已推流查询买手互动直播详情成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.enterGroup(createlive.getId(), 1);
			LiveRoomCallService.getLikeCount(createlive.getId(), 1, 2);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			liveRoomWapper.updateLiveRoomPushStatus(createlive.getId(), 7);
			sellerlivedetailBean.setLiveId(createlive.getId());
			sellerlivedetailCall.setData(sellerlivedetailBean);
			sellerlivedetailCall.callService();
			Logger.verifyEquals("200", sellerlivedetailCall.getString("Code"), "验证Code");
			verifyDB(sellerlivedetailCall.getJsonObject().get("Data").getAsJsonObject().get("LiveRoomDto").getAsJsonObject(), createlive.getId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}